package com.ruoyi.web.controller.hotel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.xc.hotel.entity.HotelRoomType;
import com.xc.hotel.service.HotelRoomTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hy
 * @version 1.0
 * @date 2020-11-18 10:59
 */
@Controller
@RequestMapping("hotel/room/type")
public class HotelRoomTypeController extends BaseController {

    private final String prefix = "hotel/roomType";

    private HotelRoomTypeService hotelRoomTypeService;

    @GetMapping
    @RequiresPermissions("hotel:room:type:view")
    public String view(@RequestParam Integer hotelId, ModelMap modelMap){
        modelMap.put("hotelId", hotelId);
        return prefix + "/table";
    }

    @GetMapping("add")
    @RequiresPermissions("hotel:room:type:add")
    public String addPage(@RequestParam String hotelId, ModelMap modelMap){
        modelMap.put("hotelId", hotelId);
        return prefix + "/add";
    }

    @PostMapping("add")
    @RequiresPermissions("hotel:room:type:add")
    @ResponseBody
    public AjaxResult add(HotelRoomType hotelRoomType){
        this.hotelRoomTypeService.save(hotelRoomType);
        return AjaxResult.success();
    }

    @GetMapping("edit/{id}")
    @RequiresPermissions("hotel:room:type:edit")
    public String editPage(@PathVariable Integer id, ModelMap modelMap){
        final HotelRoomType hotelRoomType = this.hotelRoomTypeService.getById(id);
        modelMap.put("hotelRoomType", hotelRoomType);
        return prefix + "/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @RequiresPermissions("hotel:room:type:edit")
    public AjaxResult edit(HotelRoomType hotelRoomType){
        this.hotelRoomTypeService.updateById(hotelRoomType);
        return AjaxResult.success();
    }

    @PostMapping("remove")
    @ResponseBody
    @RequiresPermissions("hotel:room:type:remove")
    public AjaxResult remove(@RequestParam List<Integer> ids){
        this.hotelRoomTypeService.removeByIds(ids);
        return AjaxResult.success();
    }

    @PostMapping("list")
    @ResponseBody
    @RequiresPermissions("hotel:room:type:view")
    public TableDataInfo list(HotelRoomType hotelRoomType){
        startPage();
        return this.getDataTable(hotelRoomTypeService.list(new QueryWrapper<>(hotelRoomType)));
    }

    @Autowired
    public void setHotelRoomTypeService(HotelRoomTypeService hotelRoomTypeService) {
        this.hotelRoomTypeService = hotelRoomTypeService;
    }
}
